package org.six11.ss.server.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.six11.util.adt.MultiState;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class UnknownHandler extends WebHandler {

  /**
   * 
   */
  public UnknownHandler(String jspFile, MultiState state) {
    super(jspFile, state);
  }

  /**
   * Tell the user that they did something horribly wrong, but don't be so harsh that you upset them
   * and they go and do something stupid involving rifles and clock towers.
   */
  @SuppressWarnings("unchecked")
  @Override
  protected void service(HttpServletRequest req, HttpServletResponse resp, ServletContext context)
      throws ServletException, IOException {
    Map params = req.getParameterMap();
    Map<String, String> spaceSeparated = new HashMap<String, String>();

    HttpSession session = req.getSession();
    for (Object key : params.keySet()) {
      String[] vals = (String[]) params.get(key);
      StringBuffer buf = new StringBuffer();
      for (String v : vals) {
        buf.append(" " + v);
      }
      spaceSeparated.put((String) key, buf.toString().trim());
    }
    session.setAttribute("things", spaceSeparated);

    go(req, resp, context);
  }

}
